layui.define(['form', 'upload'], function (exports) {
    var $ = layui.$
        , layer = layui.layer
        , laytpl = layui.laytpl
        , setter = layui.setter
        , view = layui.view
        , admin = layui.admin
        , form = layui.form
        , upload = layui.upload;
    var apiUrl = setter.apiUrl;
    var init = function () {

        var curUser = layui.data(setter.tableName).user;
        admin.req({
            url: apiUrl + '/mapi/permission/r/list' //实际使用请改成服务端真实接口
            , method: 'post'
            , data: JSON.stringify({})
            , done: function (response) {
                if (response.code === 0) {
                    var data = response.data.list;
                    var html = "";
                    for (var i = 0; i < data.length; i++) {
                        if (curUser.roleId == data[i]["id"]) {
                            html += "<option value=" + data[i]["id"] + ">" + data[i]["name"] + "</option>";
                        } else {
                            html += "<option value=" + data[i]["id"] + " disabled>" + data[i]["name"] + "</option>";
                        }
                    }
                    $("#role").append(html);
                    form.render('select');
                }
            }
        });
        form.val('LAY_set_user_info', curUser);
        form.render(); //更新全部
    }

    init();

    //自定义验证
    form.verify({
        nickname: function (value, item) { //value：表单的值、item：表单的DOM对象
            if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                return '昵称不能有特殊字符';
            }
            if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                return '昵称首尾不能出现下划线\'_\'';
            }
            if (/^\d+\d+\d$/.test(value)) {
                return '昵称不能全为数字';
            }
        }
        , pass: [
            /^[\S]{3,12}$/
            , '密码必须3到12位，且不能出现空格'
        ]
        //确认密码
        , repass: function (value) {
            if (value !== $('#LAY_password').val()) {
                return '两次密码输入不一致';
            }
        }
    });

    //网站设置
    form.on('submit(set_website)', function (data) {
        return false;
    });

    //邮件服务
    form.on('submit(set_system_email)', function (obj) {
        layer.msg(JSON.stringify(obj.field));
        return false;
    });


    //设置我的资料
    form.on('submit(setmyinfo)', function (data) {
        var uid = 0;
        if (layui.data(setter.tableName).user) {
            uid = layui.data(setter.tableName).user.id;
        }
        admin.req({
            url: apiUrl + '/mapi/user/update/u/' + uid
            , method: 'put'
            , data: JSON.stringify(data.field)
            , done: function (response) {
                if (response.code === 0) {
                    layer.msg('修改成功', {
                        offset: '15px', icon: 1, time: 1000
                    }, function () {
                        layui.data(setter.tableName, null);
                    });
                } else {
                    layer.msg("修改失败", {
                        offset: '15px', icon: 2
                    });
                }
            }
        });
    });

    //设置密码
    form.on('submit(setmypass)', function (obj) {
        var uid = layui.data(setter.tableName).user.id;
        admin.req({
            url: apiUrl + '/mapi/user/c/password/' + uid
            , method: 'put'
            , data: JSON.stringify(obj.field)
            , done: function (response) {
                if (response.code === 0) {
                    layer.msg('修改成功', {
                        offset: '15px', icon: 1, time: 1000
                    });
                    layui.data(setter.tableName, null);
                } else {
                    layer.msg("修改失败", {
                        offset: '15px', icon: 2
                    });
                }
            }
        });
    });

    //对外暴露的接口
    exports('set', {});
});